﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using DigDes.DS2012.DataLoader.Contracts;


namespace Data_Load_Reapir_Wagon
{
   
    public class Reapir:ILoader
    {
        WebClient web = new WebClient();
        Encoding enc;
        string Line;
        string buf;
        string FilePath;
        object field;
        int count=0;
        TextReader reader;
        Byter one = new Byter();
        char symb = 'P';
        

        public bool CanLoadData(string FileName, byte[] fileBytes)
        
        {
           
             buf=reader.ReadLine();
             return ((buf.Length==152)) && (fileBytes[8]==Convert.ToByte(symb));
        }

        public void LoadData(string FileName, byte[] Data)
        {
            enc = Encoding.GetEncoding(866);
            Line = enc.GetString(Data);
            reader = new StreamReader(Line);
           
                while (count != Data.Length)
                {
                    buf = reader.ReadLine();

                one.NumberWagon = Convert.ToInt16(buf.Substring(0,8));
                one.RepairIdentifier = Convert.ToInt16(buf.Substring(8,1));
                one.typeOfRepair = Convert.ToInt16(buf.Substring(9,2));
                one.Depo = Convert.ToInt16(buf.Substring(11,4));
                one.NumberVU_23 = Convert.ToInt16(buf.Substring(15,6));
                one.Road = Convert.ToInt16(buf.Substring(21,2));
                one.Station = Convert.ToInt16(buf.Substring(23,5));
                one.DateOfTransfer = IntToDate(Convert.ToInt16(buf.Substring(28,12)));
                one.fault = Convert.ToInt16(buf.Substring(40,9));
                one.DateOfConstruction = IntToDatum(Convert.ToInt16(buf.Substring(49,8)));
                one.LastDepo = Convert.ToInt16(buf.Substring(57,4));
                one.DateofLastDepo = IntToDatum(Convert.ToInt16(buf.Substring(61,8)));
                one.LastDepoofCapitalReapir = Convert.ToInt16(buf.Substring(69,4));
                one.DateofLastCapitalRepair = IntToDate(Convert.ToInt16(buf.Substring(73,8)));
                one.NumberVU_36 = Convert.ToInt16(buf.Substring(81,6));
                one.DateOfBeginRepair = IntToDate(Convert.ToInt16(buf.Substring(87,12)));
                one.DateofEndRepair = IntToDate(Convert.ToInt16(buf.Substring(99,12)));
                one.Modernization = Convert.ToInt16(buf.Substring(111,40));
                one.WagonState = Convert.ToInt16(buf.Substring(151,1));
                count+=buf.Length;

                }
}
            
           
       
        public DateTime IntToDate(int datum)
        {
            DateTime date = DateTime.ParseExact(datum.ToString(), "yyyyMMddhhmm", System.Globalization.CultureInfo.CurrentCulture);
            return date;
        }

        public DateTime IntToDatum(int datum)
        {
            DateTime date = DateTime.ParseExact(datum.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
            return date;
        }
    }

}
